import utils 

class Holiday(object):
    
    def __init__(self, gevent):
        self.title = gevent.title.text
        self.start_date, self.end_date = utils.parse_when(gevent.content)

    def includes(self, date):
        """Tests whether the given date falls within the holiday.
        """
        if self.end_date:
            return not (date < self.start_date or date > self.end_date)
        else:
            return date == self.start_date
            
def get(calendar_id, start_date='2010-01-01', end_date='2020-01-01', appengine=False):
    """Retrieves all calendar events from the calendar feed identified in the
    HOLIDAYS_FEED setting. Returns events as a list of Holiday objects.
    """
    import gdata
    import gdata.calendar.service
    calendar_service = gdata.calendar.service.CalendarService()
    if appengine:
        import gdata.alt.appengine
        gdata.alt.appengine.run_on_appengine(calendar_service)
    query = gdata.calendar.service.CalendarEventQuery(calendar_id, "public", "basic") # , 'private', 'full'
    query.start_min = str(start_date)
    query.start_max = str(end_date)
    events = []
    for entry in calendar_service.CalendarQuery(query).entry:
        events.append(Holiday(entry))
    return events